package com.ruicar.afs.cloud.afscase.cargpsmanage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.cargpsmanage.condition.CarGpsApplyCondition;
import com.ruicar.afs.cloud.afscase.cargpsmanage.entity.CarGpsApply;
import com.ruicar.afs.cloud.afscase.cargpsmanage.vo.CarGpsApplyVO;
import com.ruicar.afs.cloud.afscase.cargpsmanage.mapper.CarGpsApplyMapper;
import com.ruicar.afs.cloud.afscase.cargpsmanage.service.CarGpsApplyService;
import com.ruicar.afs.cloud.bizcommon.gps.service.GpsOLEService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.GpsStatusEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Description：GPS申请实现类</p>
 * @author xueyu.ma
 * @createDate 2020-6-15
 * @version 1.0
 */
@AllArgsConstructor
@Slf4j
@Service
public class CarGpsApplyServiceImpl extends ServiceImpl<CarGpsApplyMapper, CarGpsApply> implements CarGpsApplyService {

    private GpsOLEService gpsOLEService;

    @Override
    public IPage<CarGpsApplyVO> getCarGpsApplyInfoList(Page page, CarGpsApplyCondition carGpsApplyCondition) {
        return baseMapper.getGpsManageInfo(page,carGpsApplyCondition);
    }

    @Override
    public JSONObject getAllOrderInfoByAppCode(String orderNo) {
        return gpsOLEService.getAllOrderInfoByAppCode(orderNo);
    }

    @Override
    public List<CarGpsApply> getAllCarGpsApplyInfoByStaus(){
        return this.list(Wrappers.<CarGpsApply>lambdaQuery()
                .in(CarGpsApply::getApplyStatus, AfsEnumUtil.key(GpsStatusEnum.FIRST_TRIAL), AfsEnumUtil.key(GpsStatusEnum.PARTNERS_AUDIT)));
    }

    @Override
    public void updateApplyStatus(String contractNo, GpsStatusEnum statusEnum ,Long carId) {
        this.update(Wrappers.<CarGpsApply>lambdaUpdate()
                .eq(StringUtils.isNotBlank(contractNo), CarGpsApply::getContractNo, contractNo)
                .eq(null != carId, CarGpsApply::getCarId, carId)
                .set(CarGpsApply::getApplyStatus, AfsEnumUtil.key(statusEnum)));
    }
}
